package com.zzw.service.Impl;

import com.zzw.bean.TChargeBox;
import com.zzw.bean.TChargeBoxExample;
import com.zzw.dao.TChargeBoxMapper;
import com.zzw.service.TChargeBoxService;
import com.zzw.utils.RedisUtil;
import com.zzw.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class TChargeBoxServiceImpl implements TChargeBoxService {

    @Autowired
    private TChargeBoxMapper tChargeBoxMapper;

    @Override
    public List<TChargeBox> selectTchargeBoxList(TChargeBox tChargeBox) {
        return tChargeBoxMapper.selectTchargeBoxList(tChargeBox);
    }

    @Override
    public List<TChargeBox> listByCondition(TChargeBox tChargeBox) {
        String boxStatus = tChargeBox.getBoxStatus();
        String status = tChargeBox.getStatus();
        String isOnline = tChargeBox.getIsOnline();
        Integer id = tChargeBox.getId();

        TChargeBoxExample tChargeBoxExample = new TChargeBoxExample();
        //通过criteria对象 设置查询条件
        TChargeBoxExample.Criteria criteria = tChargeBoxExample.createCriteria();

        //查询条件不能是空白字符, 长度大于0
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (StringUtils.hasText(boxStatus)) {
            criteria.andBoxStatusEqualTo(boxStatus);
        }
        if (StringUtils.hasText(status)) {
            criteria.andStatusEqualTo(status);
        }
        if (StringUtils.hasText(isOnline)) {
            criteria.andIsOnlineEqualTo(isOnline);
        }
        //如果没有传值, 为 null, "", 或者有空白字符时, 依然是查询所有数据
        tChargeBoxExample.setOrderByClause("create_time desc");
        return tChargeBoxMapper.selectByExample(tChargeBoxExample);
    }

    @Override
    public void save(MultipartFile file, TChargeBox tChargeBox) {
        HttpServletRequest request = WebUtils.servletRequest.get();

        try {
            //1.获取完整的路径
            String fileRealPath = request.getServletContext().getRealPath(WebUtils.TCHATGE_BOX_IMG_DIRECTORY + WebUtils.getYearMonthDay());
            System.out.println("fileRealPath=" + fileRealPath);

            //2.创建这个上传的目录
            File fileRealPathDir = new File(fileRealPath);
            if (!fileRealPathDir.exists()) {
                fileRealPathDir.mkdirs();
            }

            //3.接收到提交的文件名
            String originalFilename = file.getOriginalFilename();
            System.out.println("你上传的文件名=" + originalFilename);
            originalFilename = UUID.randomUUID().toString() + "_" + System.currentTimeMillis() + "_" + originalFilename;

            //4.得到要把上传的文件保存到哪个路径[全路径: 包括文件名]
            String fileFullPath = fileRealPath + "/" + originalFilename;

            //5.创建文件
            File saveToFile = new File(fileFullPath);
            //将上传的文件, 转存到saveToFile
            file.transferTo(saveToFile);

            //6.保存新图片, 用相对路径访问
            tChargeBox.setQrCode(WebUtils.TCHATGE_BOX_IMG_DIRECTORY + WebUtils.getYearMonthDay() + originalFilename);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        tChargeBox.setCreateTime(new Date());
        tChargeBoxMapper.insertSelective(tChargeBox);
    }

    @Override
    public void update(MultipartFile file, TChargeBox tChargeBox) {
        HttpServletRequest request = WebUtils.servletRequest.get();

        try {
            //1.获取完整的路径
            String fileRealPath = request.getServletContext().getRealPath(WebUtils.TCHATGE_BOX_IMG_DIRECTORY + WebUtils.getYearMonthDay());
            System.out.println("fileRealPath=" + fileRealPath);

            //2.创建这个上传的目录
            File fileRealPathDir = new File(fileRealPath);
            if (!fileRealPathDir.exists()) {
                fileRealPathDir.mkdirs();
            }

            //3.接收到提交的文件名
            String originalFilename = file.getOriginalFilename();
            System.out.println("你上传的文件名=" + originalFilename);
            originalFilename = UUID.randomUUID().toString() + "_" + System.currentTimeMillis() + "_" + originalFilename;

            //4.得到要把上传的文件保存到哪个路径[全路径: 包括文件名]
            String fileFullPath = fileRealPath + "/" + originalFilename;

            //5.创建文件
            File saveToFile = new File(fileFullPath);
            //将上传的文件, 转存到saveToFile
            file.transferTo(saveToFile);

            //6.保存新图片, 用相对路径访问
            tChargeBox.setQrCode(WebUtils.TCHATGE_BOX_IMG_DIRECTORY + WebUtils.getYearMonthDay() + originalFilename);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        tChargeBoxMapper.updateByPrimaryKeySelective(tChargeBox);
    }

    @Override
    public void delete(Integer id) {
        tChargeBoxMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TChargeBox> getFooterData(TChargeBox tChargeBox) {
        String boxStatus = tChargeBox.getBoxStatus();
        String status = tChargeBox.getStatus();
        String isOnline = tChargeBox.getIsOnline();

        TChargeBoxExample tChargeBoxExample = new TChargeBoxExample();
        //通过criteria对象 设置查询条件
        TChargeBoxExample.Criteria criteria = tChargeBoxExample.createCriteria();

        //查询条件不能是空白字符, 长度大于0
        if (StringUtils.hasText(boxStatus)) {
            criteria.andBoxStatusEqualTo(boxStatus);
        }
        if (StringUtils.hasText(status)) {
            criteria.andStatusEqualTo(status);
        }
        if (StringUtils.hasText(isOnline)) {
            criteria.andIsOnlineEqualTo(isOnline);
        }
        // 调用 Mapper 查询汇总数据
        return tChargeBoxMapper.getFooterData(tChargeBoxExample);
    }
}
